﻿using System;
using MyBasicLib.Reflection;

namespace MyBasicLib.Data.ORMLiteNew
{
    //public interface IView
    //{
    //    /// <summary>
    //    /// 获取视图名
    //    /// </summary>
    //    /// <returns></returns>
    //    internal string GetViewName(); 
    //}

    public interface ITable
    {
        ///// <summary>
        ///// 获取表名
        ///// </summary>
        ///// <returns></returns>
        //  internal string GetTableName();
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public sealed class TableAttribute : Attribute
    {
        public String TableName { get; set; }
        public String KeyName { get; set; } //"key1,key2"
        //public Boolean ReadOnly { get; set; }
    }

    /// <summary>
    /// 用在有默认值且不能手动指定值的字段,如自增列
    /// </summary> 
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class DbGeneratedAttribute : Attribute
    {
        // public object   DefaultValue { get; set; }
    }

    internal static class MetaData
    {
        internal static object GetTableName(Type type)
        {
            return TypeDataCache.GetTypeData(type).GetCustomAttribute<TableAttribute>().TableName;
        }

        public static string GetKeyNames(Type type)
        {
            return TypeDataCache.GetTypeData(type).GetCustomAttribute<TableAttribute>().KeyName;
        }

        internal static bool IsValidTableClass(Type type)
        {
            return typeof (ITable).IsAssignableFrom(type);
        }
    }
}